package emp.servlet;

import emp.dao.UserDao;
import emp.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 叶枫
 * @date 2024/12/18.
 */

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {

    private UserDao userDao;

    @Override
    public void init() throws ServletException {
        userDao = new UserDao();
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取表单数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User login = userDao.login(username, password);

        // 简单的硬编码验证，实际应用中应该使用数据库验证
        if (login != null) {
            // 登录成功，重定向到欢迎页面
            // 跳转到用户管理页面
            response.sendRedirect("user?action=list&pageNumber=1&pageSize=10");
        } else {
            // 登录失败，重定向到登录页面并传递错误参数
            response.sendRedirect("login.jsp?error=true");
        }
    }

}
